<?php
class taoguaniostockorder_ctl_admin_iostockorder extends desktop_controller{
    var $name = "出入库管理";
    var $workground = "storage_center";

    /**
     *
     * 其他入库列表
     */
    function other_iostock(){
        $io = $_GET['io'];

        if($io){
        	$title = '其他入库';
        }else{
        	$title = '其他出库';
        }

        $params = array(
		   'actions' => array(
        		 array(
                    'label'=>'新建',
                    'href'=>'index.php?app=taoguaniostockorder&ctl=admin_iostockorder&act=iostock_add&p[0]=other&p[1]='.$io,
                    'target'=>'_blank'
                ),


            ),
                        'title'=>$title,
                        'use_buildin_new_dialog' => false,
                        'use_buildin_set_tag'=>false,
                        'use_buildin_recycle'=>false,
                        'use_buildin_export'=>false,
                        'use_buildin_import'=>false,
                        'use_buildin_filter'=>true,
            			'finder_cols'=>'name,iso_bn,oper,operator,original_bn,create_time,type_id',
          );
        /*
         * 获取操作员管辖仓库
         */
        $oBranch = &app::get('ome')->model('branch');
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids){
                $oIso = &$this->app->model('iso');
                $iso_list = $oIso->getList('iso_id', array('branch_id'=>$branch_ids), 0,-1);
                if ($iso_list){
	                foreach ($iso_list as $p){
	                    $isolist[] = $p['iso_id'];
	                }
                }
                if ($isolist){
                    $isolist = array_unique($isolist);
                    $params['base_filter']['iso_id'] = $isolist;
                }else{
                    $params['base_filter']['iso_id'] = 'false';
                }
            }else{
                $params['base_filter']['iso_id'] = 'false';
            }
        }


        $params['base_filter']['type_id'] = kernel::single('taoguaniostockorder_iostockorder')->get_create_iso_type($io,true);
        $params['base_filter']['confirm'] = 'N';

        $this->finder('taoguaniostockorder_mdl_iso', $params);
    }

	function allocate_iostock(){
        $io = $_GET['io'];
        $iostock_instance = kernel::service('ome.iostock');
        if($io){
        	$title = '调拨入库';
        	eval('$type='.get_class($iostock_instance).'::ALLOC_STORAGE;');
        }else{
        	$title = '调拨出库';
        	eval('$type='.get_class($iostock_instance).'::ALLOC_LIBRARY;');
        }


        $params = array(
                        'title'=>$title,
                        'use_buildin_new_dialog' => false,
                        'use_buildin_set_tag'=>false,
                        'use_buildin_recycle'=>false,
                        'use_buildin_export'=>false,
                        'use_buildin_import'=>false,
                        'use_buildin_filter'=>true,
       					'finder_cols'=>'name,iso_bn,oper,operator,original_bn,create_time,type_id',
          );
        /*
         * 获取操作员管辖仓库
         */
        $oBranch = &app::get('ome')->model('branch');
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids){
                $oIso = &$this->app->model('iso');
                $iso_list = $oIso->getList('iso_id', array('branch_id'=>$branch_ids), 0,-1);
                if ($iso_list){
	                foreach ($iso_list as $p){
	                    $isolist[] = $p['iso_id'];
	                }
                }
                if ($isolist){
                    $isolist = array_unique($isolist);
                    $params['base_filter']['iso_id'] = $isolist;
                }else{
                    $params['base_filter']['iso_id'] = 'false';
                }
            }else{
                $params['base_filter']['iso_id'] = 'false';
            }
        }


        $params['base_filter']['type_id'] = $type;
        $params['base_filter']['confirm'] = 'N';

        $this->finder('taoguaniostockorder_mdl_iso', $params);
    }

	function iostock_add($type,$io){
		if($io){
			$order_label = '入库单';
		}else{
			$order_label = '出库单';
		}

      	$suObj = &app::get('purchase')->model('supplier');
        $data  = $suObj->getList('supplier_id, name','',0,-1);

        $brObj = &app::get('ome')->model('branch');
        $row   = $brObj->getList('branch_id, name','',0,-1);

        /*
         * 获取操作员管辖仓库
         */
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
           $branch_list = $brObj->getBranchByUser();
        }
        $this->pagedata['branch_list']   = $branch_list;
        $is_super = 1;
        $this->pagedata['is_super']   = $is_super;



        $this->pagedata['supplier'] = $data;
        $operator = kernel::single('desktop_user')->get_name();
        $this->pagedata['operator'] = $operator;

        $this->pagedata['branch']   = $row;
        $this->pagedata['branchid']   = $branch_id;
        $this->pagedata['cur_date'] = date('Ymd',time()).$order_label;
        $this->pagedata['io'] = $io;

		/*if ( $iostock_service = kernel::service('ome.iostock') ){
            if ( method_exists($iostock_service, 'get_iostock_types') ){
                $this->pagedata['iostock_types'] = $iostock_service->get_iostock_types();
            }
        }*/

        $this->pagedata['iostock_types'] = kernel::single('taoguaniostockorder_iostockorder')->get_create_iso_type($io);

        if($io){
        	 $this->singlepage("admin/iostock/instock_add.html");
        }else{
        	 $this->singlepage("admin/iostock/outstock_add.html");
        }

    }

    function do_save_iostockorder(){
    	$this->begin("index.php?app=taoguaniostockorder&ctl=admin_iostockorder");

    	$_POST['iso_price'] = $_POST['iso_price'] ? $_POST['iso_price'] : 0;
    	$oBranchProduct = &app::get('ome')->model('branch_product');
        
        if(!$_POST['bn']) {
            $this->end(false, '请先选择入库商品！.');            
        }
        
    	$products = array();
    	foreach($_POST['bn'] as $product_id=>$bn){
            if($_POST['at'][$product_id] == 0) {
                $this->end(false, '库存数量不能为0.');
            }
            
    		if($_POST['io'] == 0){
	    		$aRow = $oBranchProduct->dump(array('product_id'=>$product_id, 'branch_id'=>$_POST['branch']),'store');
	          	if($_POST['at'][$product_id] > $aRow['store']){
	                $this->end(false, $bn.'出库数量不可大于库存数量.');
	            }
    		}

    		$products[$product_id] = array('bn'=>$bn,
    			'nums'=>$_POST['at'][$product_id],
	    		'unit'=>$_POST['unit'][$product_id],
	    		'name'=>$_POST['product_name'][$product_id],
    			'price'=>$_POST['pr'][$product_id],
    		);
    	}
    	$_POST['products'] = $products;
   		if (kernel::single('taoguaniostockorder_iostockorder')->save_iostockorder($_POST,$msg)){
            $this->end(true, '保存完成');
        }else {
            //$msg['delivery_bn'] = $dly['delivery_bn'];
            $this->end(false, '保存失败', '', array('msg'=>$msg));
        }
    }

 	/**
     *
     * 出入库单查询
     */
  function search_iostockorder(){
  	    $io = $_GET['io'];
        switch ($io)
        {
			case '1':
                $this->base_filter = array(
				);
                $this->title = '入库单查询';
                $confirm_label = '入库单确认';
                break;
            case '0':
                $this->base_filter = array(
				);
                $this->title = '出库单查询';
                $confirm_label = '出库单确认';
                break;
        }


        $this->base_filter['confirm'] = 'Y';
        $this->base_filter['type_id'] = kernel::single('taoguaniostockorder_iostockorder')->get_iso_type($io,true);

        $this->finder('taoguaniostockorder_mdl_iso',array(
           'title' => $this->title,
           'actions' => array(
              // array('label'=>$confirm_label,'submit'=>'index.php?app=ome&ctl=admin_order&act=dispatching','target'=>'dialog::{width:400,height:200,title:\'订单分派\'}'),
           ),
           'base_filter' => $this->base_filter,
           'use_buildin_new_dialog' => false,
           'use_buildin_set_tag'=>false,
           'use_buildin_recycle'=>false,
           'use_buildin_export'=>true,
           'use_buildin_import'=>false,
           'use_buildin_filter'=>true,
           'finder_cols'=>'name,iso_bn,oper,operator,original_bn,create_time,type_id',
           //'finder_aliasname'=>$finder_aliasname,
           //'finder_cols'=>$finder_cols,
        ));
    }

	function iostockorder_confirm($iso_id,$io){
		$oIso = &$this->app->model("iso");
        $oIsoItems = &$this->app->model("iso_items");
        $oProducts  = &app::get('ome')->model("products");
        $oProduct_pos = &app::get('ome')->model("branch_product_pos");
        $count = count($oIsoItems->getList('*',array('iso_id'=>$iso_id), 0, -1));
        $iso_items = $oIsoItems->getList('*',array('iso_id'=>$iso_id));
        $iso = $oIso->dump($iso_id,'branch_id,supplier_id,type_id');
        foreach($iso_items as $k=>$v){
            $product = $oProducts->dump($v['product_id'],'unit,barcode');

            //$Po_items[$k]['name'] = $v['name'];
            //$iso_items[$k]['unit'] = $product['unit'];
            $iso_items[$k]['barcode'] = $product['barcode'];
            $assign = $oProduct_pos->get_pos($v['product_id'],$iso['branch_id']);
            if(empty($assign)){
                $iso_items[$k]['is_new']="true";
            }else{
                $iso_items[$k]['is_new']="false";
            }
            $iso_items[$k]['spec_info'] = $v['spec_info'];
            $iso_items[$k]['entry_num'] = $v['nums'];
        }

        $this->pagedata['operator'] = kernel::single('desktop_user')->get_name();
        $this->pagedata['iso_items'] = $iso_items;
        $this->pagedata['iso_id'] = $iso_id;
        $this->pagedata['count']=$count;//branch_id
        $this->pagedata['branch_id']=$iso['branch_id'];
        $this->pagedata['type_id'] = $iso['type_id'];
        $this->pagedata['io'] = $io;
        if($io){
        	 $this->singlepage("admin/iostock/instock_confirm.html");
        }else{
        	 $this->singlepage("admin/iostock/outstock_confirm.html");
        }

    }

 	function save_iso_confirm(){
        $this->begin('index.php?app=taoguaniostockorder&ctl=admin_iostockorder');
        $oIsoItems = &$this->app->model("iso_items");
        $oIso = &$this->app->model("iso");
        $oBranch_pos = app::get('ome')->model("branch_pos");
        $oProduct_pos = app::get('ome')->model("branch_product_pos");
        $entry_num = $_POST['entry_num'];
        $iso_id = $_POST['iso_id'];
        $ids = $_POST['ids'];
        $branch_id = $_POST['branch_id'];
        //$pos_name = $_POST['pos_name'];
        //$pos_id = array();

        $io = $_POST['io'];
        if($io){
        	$label = '入库';
        }else{
        	$label = '出库';
        }

        if (empty($ids)){
            $this->end(false, '请选择需要'.$label.'的商品', 'index.php?app=taoguaniostockorder&ctl=admin_iostockorder&act=iostockorder_confirm&p[0]='.$iso_id.'&p[1]='.$io);
        }
        $ret = array();
		$error_bn = array();
		$oBranchProduct = &app::get('ome')->model('branch_product');
        foreach ($ids as $k=>$id) {
            if ($entry_num[$id] <= 0){
                $this->end(false, ''.$label.'量必须大于0', 'index.php?app=taoguaniostockorder&ctl=admin_iostockorder&act=iostockorder_confirm&p[0]='.$iso_id.'&p[1]='.$io);
            }

            if($io == 0){
	            $aRow = $oBranchProduct->dump(array('product_id'=>$_POST['product_ids'][$id], 'branch_id'=>$_POST['branch_id']),'store');
	            if($entry_num[$id] > $aRow['store']){
	                $this->end(false, '出库数量不可大于库存数量.');
	            }
            }

        }

 		if (kernel::single('taoguaniostockorder_iostockorder')->check_iostockorder($_POST,$msg)){
            $this->end(true, $label.'完成');
        }else {
            //$msg['delivery_bn'] = $dly['delivery_bn'];
            $this->end(false, $label.'失败',  'index.php?app=taoguaniostockorder&ctl=admin_iostockorder&act=iostockorder_confirm&p[0]='.$iso_id.'&p[1]='.$io, array('msg'=>$msg));
        }

    }

}
?>